package com.hfut.eeg.controller;

import com.hfut.eeg.common.api.CommonResult;
import com.hfut.eeg.common.component.RealTimeEEG;
import com.hfut.eeg.common.util.EEGUtil;
import com.hfut.eeg.common.util.GenerateCommon;
import com.hfut.eeg.dto.EEGReq;
import com.hfut.eeg.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.controller
 * @Description: TODO
 * @date 2019-12-11 14:42
 */
@Controller
@Api(tags = "EggController", value = "提供egg页面显示数据")
@RequestMapping("/user")
public class EEGController {

    @Autowired
    private EEGService eegService;

    @Autowired
    private BrainFFTService brainFFTService;

    @Autowired
    private BrainEnergyService brainEnergy;

    @Autowired
    private BraninChannelService braninChannelService;

    @Autowired
    private EerWavesService eerWavesService;

    @Autowired
    private RealTimeEEG realTimeEEG;

    @Autowired
    private EEGGuideService eegGuideService;
    @Autowired
    private GenerateCommon generateCommon;

    @ApiOperation("获取EEG数据")
    @RequestMapping(value = "/getEEG")
    @ResponseBody
    public CommonResult getEEG(@RequestBody @Valid EEGReq eeg){
        HashMap map = eegService.getEEGBySeg(eeg.getPatientId(),eeg.getOrder(),eeg.getNum());
        HashMap<String,Object> errWaves = eerWavesService.getEerWaves(eeg);
        int sequence = eegService.getEEGLatestSequence(eeg.getPatientId());
        if(map==null || map.get("eeg")==null){
            HashMap hashMap = eegService.getEEGBySeg(eeg.getPatientId(),sequence,1);
            hashMap.put("errWaves",errWaves.get("errWaves"));
            return CommonResult.success(hashMap);
        }
        map.put("errWaves",errWaves.get("errWaves"));
        return CommonResult.success(map);
    }

    @ApiOperation("获取FFT数据")
    @RequestMapping(value = "/getFFT")
    @ResponseBody
    public CommonResult getFFT(@RequestBody @Valid EEGReq fft){
        ArrayList<ArrayList> lists =brainFFTService.getFFTBySeg(fft.getPatientId(),fft.getOrder(),fft.getNum());
        if(lists==null){
            return CommonResult.failed("数值为空");
        }
        return CommonResult.success(lists);
    }

    @ApiOperation("获取脑部通道数据")
    @RequestMapping(value = "/getBrainChannel")
    @ResponseBody
    public CommonResult getBrainChannel(@RequestBody @Valid EEGReq channel){
        ArrayList lists = braninChannelService.getBrainChannelBySeg(channel.getPatientId(),channel.getOrder(),channel.getNum());
        if(lists==null){
            return CommonResult.failed("数值为空");
        }
        return CommonResult.success(lists);
    }

    @ApiOperation("获取脑部能量数据")
    @RequestMapping(value = "/getBrainEnergy")
    @ResponseBody
    public CommonResult getBrainEnergy(@RequestBody @Valid EEGReq energy){
        ArrayList<ArrayList> lists = brainEnergy.getBrainEnergy(energy.getPatientId(),energy.getOrder(),energy.getNum());
        if(lists==null){
            return CommonResult.failed("数值为空");
        }
        return CommonResult.success(lists);
    }

    @ApiOperation("获取异常波形数据")
    @RequestMapping(value = "/getErrWaves")
    @ResponseBody
    public CommonResult getErrWaves(@RequestBody @Valid EEGReq err){
        HashMap map= eerWavesService.getEerWaves(err);
        if(map.get("errWaves")==null){
            return CommonResult.failed("数值为空");
        }
        return CommonResult.success(map);
    }

    @ApiOperation("判断当前检查的病人是否是预期病人")
    @RequestMapping(value = "/checkEEGForPatient")
    @ResponseBody
    public CommonResult checkEEGForPatient(String patientName,String patientId){
        boolean tag = EEGUtil.checkEEGForPatient(patientName);
        if(tag){
            Thread thread = new Thread(() -> generateCommon.stimulateEEG(patientId,1,3));
            thread.start();
        }
        return CommonResult.success(tag);
    }

    @ApiOperation("获取当前实时EEG数据")
    @RequestMapping(value = "/getRealTimeEEG")
    @ResponseBody
    public CommonResult getRealTimeEEG(){
        ArrayList<ArrayList> result = EEGUtil.deepCopy(realTimeEEG.getEEG());
        if(result==null || result.size()<16 || result.get(0).size()==0){
            return CommonResult.failed("当前没有数据,请重新获取");
        }
        realTimeEEG.clear();
        return CommonResult.success(result);
    }

    @ApiOperation("获取EEG缩小图例")
    @RequestMapping(value = "/getShrinkView")
    @ResponseBody
    public CommonResult getShrinkView(@RequestBody @Valid EEGReq shrinkView){
        ArrayList<ArrayList>lists = eegService.getShrinkView(shrinkView.getPatientId(),shrinkView.getOrder(),shrinkView.getNum());
        if(lists==null||lists.size()==0){
            return CommonResult.failed("数值为空");
        }
        return CommonResult.success(lists);
    }

    @ApiOperation("获取病人单导数据")
    @RequestMapping(value = "/getSingleGuideBySeg")
    @ResponseBody
    public CommonResult getSingleGuideBySeg(@RequestBody @Valid EEGReq singleGuide){
        ArrayList<ArrayList> lists = eegGuideService.getSingleGuideBySeg(singleGuide.getPatientId(),singleGuide.getOrder(),singleGuide.getNum());
        if(lists==null || lists.size()==0){
            return CommonResult.failed("数值为空");
        }
        return CommonResult.success(lists);
    }

    @ApiOperation("获取病人均导数据")
    @RequestMapping(value = "/getAllGuideBySeg")
    @ResponseBody
    public CommonResult getAllGuideBySeg(@RequestBody @Valid EEGReq allGuide){
        ArrayList<ArrayList> lists = eegGuideService.getAllGuideBySeg(allGuide.getPatientId(),allGuide.getOrder(),allGuide.getNum());
        if(lists==null || lists.size()==0){
            return CommonResult.failed("数值为空");
        }
        return CommonResult.success(lists);
    }
}
